<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@ taglib uri="/operateTag" prefix="o"%>
<%
	String path = request.getContextPath();
	String basePath = request.getScheme() + "://"
			+ request.getServerName() + ":" + request.getServerPort()
			+ path + "/";
%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>">

<title>会员管理</title>

<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">

<!--
	<link rel="stylesheet" type="text/css" href="styles.css">
	-->
<jsp:include page="/page/head.jsp"></jsp:include>
<script type="text/javascript">
		//页面打开加载
		$(document).ready(function(){
			
		    $('#mgrTable').datagrid({
       			height: 500,
		        pageSize: 20,//每页显示的记录条数，默认为10  
		        pageList: [10,15,20],//可以设置每页记录条数的列表
				nowrap: true,
        		border: true,  
				striped: true,
				collapsible: false,
				url:path+'/member/list.do',
				sortName: 'id',
				sortOrder: 'asc',
				remoteSort: false,
				idField:'id',
				columns:[[
	                {field:'ck',checkbox:true},
	                {field:'card',title:'卡号',width:90},
	                {field:'username',title:'登陆名',width:70},
					{field:'truename',title:'用户姓名',width:70},
					{field:'mobile',title:'手机',width:90},
					{field:'score',title:'积分',width:50},
					{field:'registerTime',title:'开卡时间',width:90},
					{field:'locked',title:'状态',width:40,
						formatter:function(value,rec){
							if(value == false) return "未锁";
							return "已锁";
						}
					},
					{field:'id',title:'操作',width:200,
						formatter:function(value,rowData){
							return '<a href=\"javascript:void(0);\" onclick=\"resetPassword('+value+')\">重置密码</a> | <a href=\"javascript:void(0);\" onclick=\"openMobile('+value+','+rowData.mobile+')\">绑定手机</a>';
						}
					}
				]],
				pagination:true,
				rownumbers:true,
				toolbar:'#toolbar'
			});
			var p = $('#mgrTable').datagrid('getPager');
			$(p).pagination({   
		        beforePageText: '第',//页数文本框前显示的汉字  
		        afterPageText: '页    共  {pages} 页',
		        displayMsg: '当前显示  {from} - {to} 条记录   共  {total} 条记录'
		    });
			
		});
		
		//添加用户
		function add(){
			window.location = path+"/member/init.do?type=add";
		}
		
		
		//编辑用户
		function edit(){
			var selected = $('#mgrTable').datagrid('getSelected');
			if(selected){
				window.location = path+"/member/init.do?type=edit&userId="+selected.id;  
			}else{
				alertBox("请选择需要编辑的会员！");
			}
		}
		
		//重置密码
		function resetPassword(id){
			var submit = function (v, h, f) {
				    if (v == 'ok'){
				    	url = path +"/member/resetPassword.do?id="+id;
						$.ajax({
						  url:url,
						  success: function(data) {
							   if(data == "1"){
								   	$('#mgrTable').datagrid('reload');
									$('#mgrTable').datagrid('clearSelections');
									alertBox("操作成功！");
							   }else{
									alertBox("操作失败！");
							   }
						  }
						});
				    }
				    	
				    return true; //close
			};
			$.jBox.confirm("确定要重置该用户的密码吗？", "提示", submit)
		}
		
		//移除会员
		function remove(){
			var rows = $('#mgrTable').datagrid('getSelections');   //获取选中行信息
			
			if(rows){
				var ids = [];
				for(var i=0;i<rows.length;i++){
					ids.push(rows[i].id);
				}
				
				var submit = function (v, h, f) {
				    if (v == 'ok'){
				    	url = path +"/member/delete.do?ids="+ids.join(',')+"&t="+new Date().getTime();
						$.ajax({
						  url: url,
						  success: function(data) {
							   if(data == "1")
							   {
									$('#mgrTable').datagrid('reload');
									$('#mgrTable').datagrid('cleanSelections');
									alertBox("操作成功！");
							   } else {
									alertBox("操作失败！");
							   }
						  }
						});
				    }
				    	
				    return true;     //close
				};
				
				$.jBox.confirm("确定要删除吗？", "提示", submit);
				
			}else{
				alertBox("请选择需要删除的会员！");
			}
		}
		
		//刷新
		function refresh(){
			$('#mgrTable').datagrid('reload');
			$('#mgrTable').datagrid('clearSelections');
		}
		
		//根据条件进行模糊查询
		function search(){
			var name = $('#name').val();                      //获取筛选条件中的name
			var mobile = $('#mobile').val();                  
			var card = $('#card').val();
			var registerTimeStar = $('#registerTimeStar').datebox('getValue');                  //获取筛选条件中的开始时间
			var registerTimeEnd = $('#registerTimeEnd').datebox('getValue');                    //获取筛选条件中的结束时间
			var cardStart = $('#cardStart').val();
			$('#mgrTable').datagrid('reload',{name:name,mobile:mobile,card:card,registerTimeStar:registerTimeStar,registerTimeEnd:registerTimeEnd,cardStart:cardStart});
		}
		
		function clean(){
			$('#name').attr('value','');
			$('#mobile').attr('mobile','');
			$('#card').attr('card','');
			$('#registerTimeStar').datebox('setValue','');
			$('#registerTimeEnd').datebox('setValue','');
			$('#cardStart').attr('cardStart','');
		}
		
		
		
		
		
		/* 生成jbox对话窗口 */
		function openMobile(id,mobile){
			
            var html = "<div style='padding:10px;'>"+
		               "原手机号码：&nbsp;&nbsp;<input disabled='disabled' type='text' id='Omobile' name='Omobile' disabled='disabled' value='"+mobile+"' /><br/>"+
					   "新手机号码:&nbsp;&nbsp;&nbsp;<input type='text' id='mobile' name='mobile' /><br/>"+
					   "重复手机号码：<input type='text' id='Rmobile' name='Rmobile' />"+
					   "</div>";
			   
			var data = {};    //不可去除，一旦去除，提交按钮将失效
            var states = {};
            states.state1 = {
                content: html,
                buttons: { '绑定': 1, '取消': 0 },
                submit: function (v, h, f) {
                    if (v == 0) {
                        return true; // 关闭窗口
                    }
                    else {
                        h.find('.errorBlock').hide('fast', function () { $(this).remove(); });

                        data.mobile = f.mobile; //或 h.find('#mobile').val();    //获取文本框输入的值
                        if (data.mobile == '' || parseInt(data.mobile) < 1) {
                            $('<div class="errorBlock" style="display: none;"><font color="red" size="2">请输入新手机号！</font></font></div>').prependTo(h).show('fast');
                            return false;
                        }
                        
                        //输入手机号码正确性验证
                        if(mobileExtend(data.mobile) == false){
                        	$('<div class="errorBlock" style="display: none;"><font color="red" size="2">您输入的手机号码格式不正确！</font></div>').prependTo(h).show('fast');
                        	return false;
                        }
                        
                        //重复手机号码正确性验证
                        data.Rmobile = f.Rmobile;
                        if (data.Rmobile == ''||data.Rmobile != data.mobile) {
                            $('<div class="errorBlock" style="display: none;"><font color="red" size="2">请输入正确的重复手机号码 ！</font></div>').prependTo(h).show('fast');
                            return false;
                        }
                        
                        //验证通过，前往后台处理
                        $.ajax({
  						  url: path +"/user/jBoxUpdateUserMobile.do?mobile="+f.mobile+"&userId="+id,    //将新手机号码传递过去
  						  success: function(data) {
  							   if(data=="1"){
  								    $.jBox.nextState(); //前往提示阶段
  									//反馈一些信息提示，做if判断，如果返回的结果是成功的，提示更换手机号码成功了，如果返回的结果失败，就提示处理失败
  			                        var msg = [];
  						            msg.push('<div class="msg-div">');
  						            msg.push('<p>操作成功!</p>');    //提示信息主显示栏
  						            msg.push('</div>');
	  		                        window.setTimeout(function () { $.jBox.nextState(msg.join('')); }, 2000);    //设定下一个窗口出现的时间
	  		                        //刷新页面
	  		                        $('#mgrTable').datagrid('reload');
	  		        			    $('#mgrTable').datagrid('clearSelections');
  							   }else{
  								    $.jBox.nextState(); 
			                        var msg = [];
						            msg.push('<div class="msg-div">');
						            msg.push('<p>操作失败!</p>');    
						            msg.push('</div>');
	  		                        window.setTimeout(function () { $.jBox.nextState(msg.join('')); }, 2000);    //设定下一个窗口出现的时间
  							   }
  						  }
  						});
                        
                      
                    }

                    return false;
                }
            };
            states.state2 = {
                content: '<div class="msg-div">正在提交...</div>',
                buttons: {}   // no buttons
            };
            states.state3 = {
            	    content: '',
            	    buttons: { '确定': 0 }
            };

            $.jBox.open(states, '用户绑定手机', 400, 'auto');
		}
		
		//手机号码验证
		function mobileExtend(mobile){
			//return /^[1][3-8]+d{9}/.test(mobile) ; 
			return /^1[3|4|5|8][0-9]\d{4,8}$/.test(mobile);
		}
		
	</script>

</head>

<body>
	<div class="right">
		<h1>
			<b>会员管理</b><span><img
				src="<%=basePath%>images/right_h1bg2.jpg" width="3" height="29" /></span>
		</h1>
		<div class="ri_main">
			<table width="99%" border="0" cellspacing="0" cellpadding="4">
				<tr>
					<td style="vertical-align: middle;">
						<label>会员名称：</label><input class="easyui-validatebox" type="text" name="name" id="name" /> 
						<label>会员手机：</label><input class="easyui-validatebox" type="text" name="mobile" id="mobile" />
						<label>会员卡号：</label><input class="easyui-validatebox" type="text" name="card" id="card" /><br />
						<label>开卡时间：</label><input class="easyui-datetimebox" type="text" name="registerTimeStar" id="registerTimeStar" />
						&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<label>到</label>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<input class="easyui-datetimebox" type="text" name="registerTimeEnd" id="registerTimeEnd" />
						&nbsp;<label>所属商家：</label><input class="easyui-validatebox" type="text" name="cardStart" id="cardStart" /> 
						<input type="button" onclick="search()" value="查询" />
						<input type="button" onclick="clean()" value="清除" /></td>
				</tr>
			</table>
			<div id="toolbar">
				<a href="javascript:void(0);" onclick="add()"
					class="easyui-linkbutton" iconCls="icon-add">新增</a> <a
					href="javascript:void(0);" onclick="edit()"
					class="easyui-linkbutton" iconCls="icon-edit">编辑</a> <a
					href="javascript:void(0);" onclick="remove()"
					class="easyui-linkbutton" iconCls="icon-remove">删除</a> <a
					href="javascript:void(0);" onclick="refresh()"
					class="easyui-linkbutton" iconCls="icon-reload">刷新</a>
			</div>
			<table id="mgrTable">
			
			</table>
		</div>
	</div>
</body>
</html>
